package com.shrfid.rfidsdk.uitls;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//


public class GFunction {
    private static final char[] DIGITS_LOWER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    private static final char[] DIGITS_UPPER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public GFunction() {
    }

    public static char[] encodeHex(byte[] data) {
        return encodeHex(data, true);
    }

    public static char[] encodeHex(byte[] data, int dataLen) {
        return encodeHex(data, dataLen, true);
    }

    public static char[] encodeHex(byte[] data, boolean toLowerCase) {
        return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
    }

    public static char[] encodeHex(byte[] data, int dataLen, boolean toLowerCase) {
        return encodeHex(data, dataLen, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
    }

    protected static char[] encodeHex(byte[] data, char[] toDigits) {
        int len = data.length;
        char[] out = new char[len << 1];
        int i = 0;

        for(int var5 = 0; i < len; ++i) {
            out[var5++] = toDigits[(240 & data[i]) >>> 4];
            out[var5++] = toDigits[15 & data[i]];
        }

        return out;
    }

    protected static char[] encodeHex(byte[] data, int dataLen, char[] toDigits) {
        int len = dataLen;
        char[] out = new char[dataLen << 1];
        int i = 0;

        for(int var6 = 0; i < len; ++i) {
            out[var6++] = toDigits[(240 & data[i]) >>> 4];
            out[var6++] = toDigits[15 & data[i]];
        }

        return out;
    }

    public static String encodeHexStr(byte[] data) {
        return encodeHexStr(data, false);
    }

    public static String encodeHexStr(byte[] data, int dataLen) {
        return encodeHexStr(data, dataLen, false);
    }

    public static String encodeHexStr(byte[] data, boolean toLowerCase) {
        return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
    }

    public static String encodeHexStr(byte[] data, int dataLen, boolean toLowerCase) {
        return encodeHexStr(data, dataLen, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
    }

    protected static String encodeHexStr(byte[] data, char[] toDigits) {
        return new String(encodeHex(data, toDigits));
    }

    protected static String encodeHexStr(byte[] data, int dataLen, char[] toDigits) {
        return new String(encodeHex(data, dataLen, toDigits));
    }

    public static byte[] decodeHex(char[] data) {
        int len = data.length;
        if ((len & 1) != 0) {
            return null;
        } else {
            byte[] out = new byte[len >> 1];
            int i = 0;

            for(int j = 0; j < len; ++i) {
                int f1 = toDigit(data[j], j) << 4;
                if (f1 < 0) {
                    return null;
                }

                ++j;
                int f2 = toDigit(data[j], j);
                if (f2 < 0) {
                    return null;
                }

                ++j;
                out[i] = (byte)((f1 | f2) & 255);
            }

            return out;
        }
    }

    public static byte[] decodeHex(char[] data, int dataLen) {
        int len = dataLen;
        if ((dataLen & 1) != 0) {
            return null;
        } else {
            byte[] out = new byte[dataLen >> 1];
            int i = 0;

            for(int j = 0; j < len; ++i) {
                int f1 = toDigit(data[j], j) << 4;
                if (f1 < 0) {
                    return null;
                }

                ++j;
                int f2 = toDigit(data[j], j);
                if (f2 < 0) {
                    return null;
                }

                ++j;
                out[i] = (byte)((f1 | f2) & 255);
            }

            return out;
        }
    }

    public static byte[] decodeHex(String sData) {
        if (sData.equals("")) {
            return null;
        } else {
            char[] data = sData.toCharArray();
            int len = data.length;
            if ((len & 1) != 0) {
                return null;
            } else {
                byte[] out = new byte[len >> 1];
                int i = 0;

                for(int j = 0; j < len; ++i) {
                    int t1 = toDigit(data[j], j) << 4;
                    if (t1 < 0) {
                        return null;
                    }

                    ++j;
                    int t2 = toDigit(data[j], j);
                    if (t2 < 0) {
                        return null;
                    }

                    ++j;
                    out[i] = (byte)((t1 | t2) & 255);
                }

                return out;
            }
        }
    }

    protected static int toDigit(char ch, int index) {
        int digit = Character.digit(ch, 16);
        return digit;
    }

    public static void BytesReverse(byte[] buf, int len) {
        for(int i = 0; i < len / 2; ++i) {
            byte tmp = buf[i];
            buf[i] = buf[len - 1 - i];
            buf[len - 1 - i] = tmp;
        }

    }
}
